using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;


public class Notice : MonoBehaviour, IBeginDragHandler,IDragHandler,IEndDragHandler
{
    private GameObject m_DraggingIcon;
    private RectTransform m_DraggingPlane;
    public bool dragOnSurfaces = true;
    public Transform Decorations_front;

    private Vector3 mousePosition;
    
    public Transform wall_BottomLeftCorner;
    public Transform wall_TopRightCorner;
    
    private void Update()
    {
        mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        if (NPCManager.instance.isInAskMode)
        {
            return;
        }
        
        var canvas = FindInParents<Canvas>(gameObject);
        if (canvas == null)
            return;
//
        // We have clicked something that can be dragged.
        // What we want to do is create an icon for this.
        m_DraggingIcon = gameObject;

        if (dragOnSurfaces)
            m_DraggingPlane = transform as RectTransform;
        else
            m_DraggingPlane = canvas.transform as RectTransform;
        DecorationLayering();

    }
    
    public void OnDrag(PointerEventData data)
    {
        if (NPCManager.instance.isInAskMode)
        {
            return;
        }
        
        if (m_DraggingIcon != null)
            SetDraggedPosition(data);
        DecorationLayering();

    }
    
    

    public void OnEndDrag(PointerEventData eventData)
    {
        if (NPCManager.instance.isInAskMode)
        {
            return;
        }
        
        SetDraggedPosition(eventData);
        DecorationLayering();
    }

    static public T FindInParents<T>(GameObject go) where T : Component
    {
        if (go == null) return null;
        var comp = go.GetComponent<T>();

        if (comp != null)
            return comp;

        Transform t = go.transform.parent;
        while (t != null && comp == null)
        {
            comp = t.gameObject.GetComponent<T>();
            t = t.parent;
        }
        return comp;
    }
    
    private void SetDraggedPosition(PointerEventData data)
    {
        if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)
            m_DraggingPlane = data.pointerEnter.transform as RectTransform;

        var rt = m_DraggingIcon.GetComponent<RectTransform>();
        Vector3 globalMousePos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, data.position, data.pressEventCamera, out globalMousePos))
        {
            rt.position = globalMousePos;
            Vector3 tempPosition = globalMousePos;
            if (mousePosition.x < wall_BottomLeftCorner.position.x)
            {
                tempPosition.x = wall_BottomLeftCorner.position.x;
            }
            
            if (mousePosition.x > wall_TopRightCorner.position.x)
            {
                tempPosition.x = wall_TopRightCorner.position.x;
            }

            if (mousePosition.y < wall_BottomLeftCorner.position.y)
            {
                tempPosition.y = wall_BottomLeftCorner.position.y;
            }

            if (mousePosition.y > wall_TopRightCorner.position.y)
            {
                tempPosition.y = wall_TopRightCorner.position.y;
            }

            rt.position = tempPosition;
            rt.rotation = m_DraggingPlane.rotation;
        }
    }
    
    public void DecorationLayering()
    {
        transform.parent = Decorations_front;
        transform.SetSiblingIndex(2);
    }
   
}

